package com.projectsexception.festivales.provider;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import com.projectsexception.festivales.provider.listeners.DownloaderListener;

public class Downloader {
    
    /**
     * Utility method to download files. It have to check if there is an update
     * @param file file where store the downloaded file. It can exists or not
     * @param URL URL to download from
     * @throws IOException 
     */
    public void dowloadFile(File file, String URL) throws IOException {
        dowloadFile(file, URL, null);
    }
    
    /**
     * Utility method to download files. It have to check if there is an update
     * @param file file where store the downloaded file. It can exists or not
     * @param URL URL to download from
     * @param listener listener
     * @throws IOException 
     */
    public void dowloadFile(File file, String URL, DownloaderListener listener) throws IOException {
        URL urlObj = new URL(URL);
        URLConnection conn = urlObj.openConnection();
        
        if (conn instanceof HttpURLConnection) {
            
            if (listener != null) {
                listener.establishingConnection();
            }
            
            HttpURLConnection httpConn = (HttpURLConnection) conn;
            httpConn.setAllowUserInteraction(false);
            httpConn.setInstanceFollowRedirects(true);
            httpConn.setRequestMethod("GET");
            if (file.exists()) {
                long lastModified = file.lastModified();
                httpConn.setIfModifiedSince(lastModified);
            }
            httpConn.connect();

            int response = httpConn.getResponseCode();            
            if (listener != null) {
                listener.receivedResponse(response);
            }            
            if (response == HttpURLConnection.HTTP_NOT_MODIFIED) {
                System.out.println("Not modified received from server");
            } else if (response == HttpURLConnection.HTTP_OK) {
                if (listener != null) {
                    listener.dowlonadStarted();
                }
                System.out.println("File found, proceed to download");
                InputStream stream = httpConn.getInputStream();
                BufferedInputStream in = new BufferedInputStream(stream);
                BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
                int i;
                while ((i = in.read()) != -1) {
                    out.write(i);
                }
                out.flush();
                out.close();
                stream.close();
                if (listener != null) {
                    listener.dowlonadFinished();
                }
            } else {
                System.out.println("Received " + response + " from server for file " + URL);
            }
            httpConn.disconnect();
        }
    }

}
